package com.boarsoft.flow.store;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.flow.core.SimpleFlow;

/**
 * 此类为 SimpleFlowPersister 的空实现（默认实现）
 * 
 * @author Mac_J
 *
 */
public class SimpleFlowPersisterImpl implements SimpleFlowPersister {
	/** 用于临时保存挂起的流程 */
	protected Map<String, Map<String, SimpleFlow>> flowMap = //
			new ConcurrentHashMap<String, Map<String, SimpleFlow>>();

	@Override
	public void save(SimpleFlow o) {
		String flowId = o.getFlowId();
		String id = o.getId();
		Map<String, SimpleFlow> m = flowMap.get(flowId);
		if (m == null) {
			synchronized (flowMap) {
				m = flowMap.get(flowId);
				if (m == null) {
					m = new ConcurrentHashMap<String, SimpleFlow>();
					flowMap.put(flowId, m);
				}
			}
		}
		m.put(id, o);
	}

	@Override
	public boolean update(SimpleFlow o) {
		return false;
	}

	@Override
	public List<SimpleFlow> list(int revMax1, int revMax2, int revMax3) {
		return null;
	}

	@Override
	public SimpleFlow load(String flowId, String exeId) {
		Map<String, SimpleFlow> m = this.flowMap.get(flowId);
		if (m == null) {
			return null;
		}
		return m.get(exeId);
	}

	@Override
	public void delete(SimpleFlow flow) {
		this.remove(flow.getFlowId(), flow.getId());
	}

	@Override
	public SimpleFlow remove(String flowId, String exeId) {
		Map<String, SimpleFlow> m = this.flowMap.get(flowId);
		if (m == null) {
			return null;
		}
		return m.remove(exeId);
	}

	@Override
	public PagedResult<SimpleFlow> list(String flowId, String key, int pageNo, int pageSize) {
		return null;
	}
}
